String.prototype.getQueryStringValue = function (name) {
  if (!this || !name) return;
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == name) {
      return pair[1];
    }
  }
  return false;
};

Array.prototype.toTree = function () {
  let that = this;
  let map = {},
    node,
    result = [],
    i;
  for (i = 0; i < that.length; i += 1) {
    map[that[i].id] = i;
    // 要讲children初始化为数组
    that[i].children = [];
  }
  for (i = 0; i < that.length; i++) {
    node = that[i];
    if (node.parentId !== null) {
      // 这里通过map来得到父节点的位置
      let parent = that[map[node.parentId]];
      if (parent) that[map[node.parentId]].children.push(node);
      else result.push(node);
    } else {
      result.push(node);
    }
  }
  return result;
};

Array.prototype.remove = function (val) {
  if (Array.isArray(val)) {
    for (let i = 0; i < val.length; i++) {
      this.remove(val[i]);
    }
  } else {
    let index = this.indexOf(val);
    if (index > -1) {
      this.splice(index, 1);
    }
  }
  return this;
};

Number.prototype.toMoney = function () {
  const digitsRE = /(\d{3})(?=\d)/g;
  let value = typeof this === "String" ? parseFloat(value) : this;
  if (!isFinite(value) || (!value && value !== 0)) return "";
  let decimals = 2;
  let stringified = Math.abs(value).toFixed(decimals);
  let _int = decimals ? stringified.slice(0, -1 - decimals) : stringified;
  let i = _int.length % 3;
  let head = i > 0 ? _int.slice(0, i) + (_int.length > 3 ? "," : "") : "";
  let _float = decimals ? stringified.slice(-1 - decimals) : "";
  let sign = value < 0 ? "-" : "";
  return sign + head + _int.slice(i).replace(digitsRE, "$1,") + _float;
};

Number.prototype.toDecimal = function (num) {
  if (this == undefined || this == null || this.toString().trim() == "") {
    return "";
  }
  if (isNaN(this)) {
    return new Number(0).toDecimal(num);
  }
  let per = 100;
  if (num == undefined) {
    num = 2;
  }
  if (num == 0) {
    let numberString = String(this);
    if (numberString.indexOf(".") > -1) {
      return numberString.split(".")[0];
    } else {
      return numberString;
    }
  } else if (num == 3) {
    per = 1000;
  } else if (num == 4) {
    per = 10000;
  } else if (num == 5) {
    per = 100000;
  } else if (num == 6) {
    per = 1000000;
  } else if (num == 7) {
    per = 10000000;
  } else if (num == 8) {
    per = 100000000;
  } else if (num == 9) {
    per = 1000000000;
  } else if (num == 10) {
    per = 10000000000;
  }
  let f_x = parseFloat(this);
  if (isNaN(f_x)) {
    alert("function:changeTwoDecimal->parameter error");
    return false;
  }
  if (f_x > 0) {
    f_x = f_x + 0.00000000001;
  }
  f_x = Math.round(f_x * per) / per;
  let s_x = f_x.toString();
  let pos_decimal = s_x.indexOf(".");
  if (pos_decimal < 0) {
    pos_decimal = s_x.length;
    s_x += ".";
  }
  while (s_x.length <= pos_decimal + num) {
    s_x += "0";
  }
  return s_x;
};

Number.prototype.toPrice = function () {
  if (!this) return null;
  return this.toDecimal(6);
};

Number.prototype.toQuantity = function () {
  if (!this) return null;
  return this.toDecimal(6);
};

String.prototype.toMoney = function () {
  if (!this || this == 0) return "";
  let value = parseFloat(this);
  return value.toMoney();
};

String.prototype.toPrice = function () {
  if (!this || this == 0) return "";
  let value = parseFloat(this);

  return value.toDecimal(6);
};

String.prototype.toQuantity = function () {
  if (!this || this == 0) return "";
  let value = parseFloat(this);

  return value.toDecimal(6);
};


Date.prototype.toPeriod = function () {
  let year = this.getFullYear();
  let month = this.getMonth() + 1;

  return year * 100 + month;
};

Date.prototype.nowYearMonth = function () {
  let date = new Date();
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  if (month < 10) month = `0${month}`;

  return `${year}-${month}`;
};

Date.prototype.format = function (format) {
  let date = this;
  const o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
    "H+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds(), // 毫秒
    a: date.getHours() < 12 ? "上午" : "下午", // 上午/下午
    A: date.getHours() < 12 ? "AM" : "PM", // AM/PM
  };
  if (/(y+)/.test(format)) {
    format = format.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return format;
};
